﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using Common.Model;
using Common.DataAccess;

namespace Common.ServiceLogic
{
    public class GenericEntityManagerServiceLogic<E, C, EV, DA> : IGenericEntityManagerServiceLogic<E, C, EV>
        where E : IObjectWithChangeTracker
        where C : CriteriaEntity
        where EV : ViewEntity
        where DA : IGenericEntityManagerDataAccess<E, C, EV>, new()
    {

        #region IEntityManagerService<E,C,EV> Members

        DA _dataAccess;
        protected DA DataAccess
        {
            get
            {
                if (_dataAccess == null)
                {
                    _dataAccess = new DA();
                }
                return _dataAccess;
            }
        }

        public virtual E Read(Guid id)
        {
            return DataAccess.Read(id);
        }

        public virtual List<EV> Read(C criteria)
        {
            return DataAccess.Read(criteria);
        }

        public virtual void Save(E entity)
        {
            DataAccess.Save(entity);
        }

        public virtual void Delete(ViewEntity viewEntity)
        {
            DataAccess.Delete(viewEntity);
        }

        public virtual List<E> ReadEntities()
        {
            return DataAccess.ReadEntities();
        }

        public void RejectChanges()
        {
            DataAccess.RejectChanges();
        }

        public virtual List<E> ReadEntitiesByParameter(IObjectWithChangeTracker entityObject)
        {
            return DataAccess.ReadEntitiesByParameter(entityObject);
        }

        #endregion
    }
}